package com.iksanika.controls4j.components;

import com.devunion.bogdan.view.util.DialogHelper;
import com.devunion.bogdan.view.util.i18n;

import javax.swing.*;
import java.io.File;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * @author Viktar Liaskovich
 */
public class JPathChooser extends JComponent {
    protected JLabel jLabelTitle;
    protected JTextField jTextFieldPath;
    protected JButton jButtonChoose;

    private File currentDirectory;

    /**
     *  
     */
    private boolean askForCreation = true;

    public JPathChooser(String title) {
        init(title);
    }

    public boolean isAskForCreation() {
        return askForCreation;
    }

    public void setAskForCreation(boolean askForCreation) {
        this.askForCreation = askForCreation;
    }

    public void setCurrentDirectory(File currentDirectory) {
        if (currentDirectory == null) {
            throw new IllegalArgumentException("CurrentDirectory can't be null");
        }

        this.currentDirectory = currentDirectory;
        jTextFieldPath.setText(currentDirectory.getAbsolutePath());
    }

    private void init(String title) {
        setLayout(new GridBagLayout());

        jLabelTitle = new JLabel(title);
        jTextFieldPath = new JTextField();
        jTextFieldPath.setName("PATH");

        jButtonChoose = new ChooseButton();
        jButtonChoose.setPreferredSize(new Dimension(32, jTextFieldPath.getPreferredSize().height+1));

        if (currentDirectory != null) {
            jTextFieldPath.setText(currentDirectory.getAbsolutePath());
        }

        DialogHelper.placeComponent(this, jLabelTitle, 0, 0, 1, 1,
                0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, 0, 0, 0, 4);

        DialogHelper.placeComponent(this, jTextFieldPath, 0, 1, 1, 1,
                1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, 1, 0, 0, 4);

        DialogHelper.placeComponent(this, jButtonChoose, 1, 1, 1, 1,
                0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, 0, 4, 0, 4);
    }

    public String getSelectedPath() {
        return jTextFieldPath.getText().trim();
    }

    private class ChooseButton extends JButton {
        private JFileChooser jFileChooser;

        public ChooseButton() {
            addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (jFileChooser == null) {
                         jFileChooser = getDirChooser();
                    }

                    if (jFileChooser.showDialog(ChooseButton.this, "Ok") == JFileChooser.APPROVE_OPTION) {
                        File file = jFileChooser.getSelectedFile();

                        if (file != null) {
                            jTextFieldPath.setText(file.getAbsolutePath());
                        }
                    }
                }
            });
        }


        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            int x = getWidth()/2;
            int y = getHeight()/2;

            g.drawOval(x-4, y, 1, 1);
            g.drawOval(x  , y, 1, 1);
            g.drawOval(x+4, y, 1, 1);
        }

        private JFileChooser getDirChooser() {
            final JFileChooser result;

            if (currentDirectory == null) {
                result = new JFileChooser();
            } else {
                result = new JFileChooser(currentDirectory);
            }

            result.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            result.setDialogTitle(i18n.getString("Select path"));
            result.setMultiSelectionEnabled(false);

            result.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (JFileChooser.APPROVE_SELECTION.equals(e.getActionCommand())) {
                        if (!result.getSelectedFile().exists()) {
                            if (askForCreation && askForCreation()) {
                                if (!result.getSelectedFile().mkdirs()) {
                                    showWarning();
                                }
                            }
                        }
                    }
                }

                private void showWarning() {
                    JOptionPane.showMessageDialog(result, "Can't create specified directory. Please, choose another name", "Warning", JOptionPane.WARNING_MESSAGE);
                }

                private boolean askForCreation() {
                    return JOptionPane.showConfirmDialog(result, "Selected Directory doesn't exists. Do you want to create it?", "", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
                }
            });
            return result;
        }
    }
}
